package com.ggh.jinjilive.remotedata.systemapi;

import android.content.Context;

import androidx.lifecycle.ViewModelProvider;

import com.ggh.jinjilive.remotedata.BaseApiModule;
import com.ggh.jinjilive.remotedata.BaseInterceptor;
import com.ggh.jinjilive.remotedata.ViewModelFactory;
import com.ggh.jinjilive.utils.HttpsUtils;

import org.apache.http.conn.ssl.SSLSocketFactory;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class SystemApiServiceModule extends BaseApiModule {
    private Context context;
    private Retrofit retrofit = null;

    public SystemApiServiceModule(Context context) {
        this.context = context.getApplicationContext();
    }

    @Provides
    @Singleton
    public Retrofit provideRetrofit() {
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();

        okHttpClientBuilder.hostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        okHttpClientBuilder.addInterceptor(new BaseInterceptor());
        okHttpClientBuilder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
        //连接时间
        okHttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
        //写时间
        okHttpClientBuilder.writeTimeout(30, TimeUnit.SECONDS);
        //读写时间
        okHttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
        OkHttpClient client = okHttpClientBuilder.build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(SYSTEM_URL)
                .build();
        return retrofit;
    }

    @Provides
    @Singleton
    SystemApiService getApiCallInterface(Retrofit retrofit) {
        return retrofit.create(SystemApiService.class);
    }


    @Provides
    @Singleton
    ViewModelProvider.Factory getViewModelFactory(SystemApiService myRepository) {
        return new ViewModelFactory(myRepository);
    }
}
